﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using BySoft.Server.Common;
namespace BySoft.Server.Business
{
    public partial class ClsComm
    {
        #region 更新单据
        /// <summary>
        /// 只取不更新
        /// </summary>
        /// <param name="sequenceName"></param>
        /// <param name="strSign"></param>
        /// <param name="iLength"></param>
        /// <param name="broker"></param>
        /// <returns></returns>
        public static string GetMaxSequenceID(string sequenceName, string strSign, int iLength, DataAccessBroker broker)
        {
            int id = ClsCommDA.GetMaxSequenceID(sequenceName, broker);
            string strid = id.ToString().PadLeft(iLength, '0');
            return strSign + strid;
        }

        /// <summary>
        /// 只取不更新
        /// </summary>
        /// <param name="sequenceName"></param>
        /// <param name="strSign"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public static string GetMaxSequenceID(string sequenceName, string strSign, int iLength)
        {
            int id = ClsCommDA.GetMaxSequenceID(sequenceName);
            string strid = id.ToString().PadLeft(iLength, '0');
            return strSign + strid;
        }

        /// <summary>
        /// 取且更新
        /// </summary>
        /// <param name="sequenceName"></param>
        /// <param name="strSign"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public static string GetAutalMaxSequenceID(string sequenceName, string strSign, int iLength)
        {
            int id = ClsCommDA.GetAutalMaxSequenceID(sequenceName);
            string strid = id.ToString().PadLeft(iLength, '0');
            return strSign + strid;
        }

        /// <summary>
        /// 取且更新
        /// </summary>
        /// <param name="sequenceName"></param>
        /// <param name="strSign"></param>
        /// <param name="iLength"></param>
        /// <param name="broker"></param>
        /// <returns></returns>
        public static string GetAutalMaxSequenceID(string sequenceName, string strSign, int iLength, DataAccessBroker broker)
        {
            int id = ClsCommDA.GetAutalMaxSequenceID(sequenceName, broker);
            string strid = id.ToString().PadLeft(iLength, '0');
            return strSign + strid;
        }

        /// <summary>
        /// 单据号值+１
        /// </summary>
        /// <param name="sequenceName"></param>
        /// <param name="broker"></param>
        public static void SetMaxSequenceID(string sequenceName, DataAccessBroker broker)
        {
            ClsCommDA.SetMaxSequenceID(sequenceName, broker);
        }

        /// <summary>
        /// 单据号值+１
        /// </summary>
        /// <param name="sequenceName"></param>
        public static void SetMaxSequenceID(string sequenceName)
        {
            ClsCommDA.SetMaxSequenceID(sequenceName);
        }
        #endregion

        public static bool CheckExistNoMainKey(string strTbl, string strMainKey, string strKeyValue, Dictionary<string, string> dc)
        {
            return ClsCommDA.CheckExistNoMainKey(strTbl, strMainKey, strKeyValue, dc);
        }

        public static bool CheckExistNoMainKey(string strTbl, string strMainKey, string strKeyValue, string strNameKey,string strNameValue)
        {
            return ClsCommDA.CheckExistNoMainKey(strTbl, strMainKey, strKeyValue, strNameKey, strNameValue);
        }

        public static DataTable SelectGetField(string tableName)
        {
            return ClsCommDA.SelectGetField(tableName);
        }

        public static DataTable SelectProcessQuantity(string ProductInfoMcod)
        {
            return ClsCommDA.SelectProcessQuantity(ProductInfoMcod);
        }

        public static DataTable SelectCheckOrder(string UserID, string WorkGroupName, string BillID)
        {
            return ClsCommDA.SelectCheckOrder(UserID, WorkGroupName, BillID);
        }

        public static bool CheckExist(string strTbl, string strMainKey, string strKeyValue, bool bStatus)
        {
            return ClsCommDA.CheckExist(strTbl, strMainKey, strKeyValue, bStatus);
        }

        public static DataTable ExcelDataSource(string filepath)
        {
            return ClsCommDA.ExcelDataSource(filepath, "Sheet1");
        }

        public static bool CheckNameWithoutID(string strTbl, string strMainKey, string strKeyValue, string strNameKey, string strName, bool bStatus)
        {
            return ClsCommDA.CheckNameWithoutID(strTbl, strMainKey, strKeyValue, strNameKey, strName, bStatus);
        }

        public static void MoveTop(string strTbl, string strOrderField, string strMainKey, string strMainKeyValue)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.FillOrderNoWithNull(strTbl, strMainKey, strOrderField, broker);
                    ClsCommDA.MoveTop(strTbl, strOrderField, strMainKey, strMainKeyValue, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static bool SwitchOrder(string strType, string strOriCode, string strTbl, string strMainKey, string strOrderField, string strOrderValue, string strWhere2, string strWhere2Value, string strWhere3, string strWhere3Value)
        {
            bool b = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.FillOrderNoWithNull(strTbl, strMainKey, strOrderField, broker);
                    string strOtherCode = ClsCommDA.SelectMainKeyWithOtherOrder(strType, strTbl, strMainKey, strOrderField, strOrderValue, strWhere2, strWhere2Value, strWhere3, strWhere3Value, broker);
                    if (!string.IsNullOrEmpty(strOtherCode))
                    {
                        //移动的行与被告移行互换排序号
                        ClsCommDA.SetOrderNo(strTbl, strMainKey, strOriCode, strOtherCode, strOrderField, strOrderValue, broker);
                        b = true;
                    }
                    else
                    {
                        b = false;
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
            return b;
        }

        public static bool IsExistCheckUser(string strCheckType,string strUserID)
        {
            return ClsCommDA.IsExistCheckUser(strCheckType, strUserID);
        }

        public static bool IsExistAuditUser(string strModule, string strUserID)
        {
            return ClsCommDA.IsExistAuditUser(strModule, strUserID);
        }

        public static bool IsExistAudit(string strModule)
        {
            return ClsCommDA.IsExistAudit(strModule);
        }

        public static bool IsUserHaveCheck(string strCheckType, string strMainKey, string strUserID)
        {
            return ClsCommDA.IsUserHaveCheck(strCheckType, strMainKey, strUserID);
        }

        //public static void SaveCheckRecord(SysBillcheckrecordsEntity entity)
        //{
        //    using (DataAccessBroker broker = DataAccessFactory.Instance())
        //    {
        //        try
        //        {
        //            broker.BeginTransaction();
        //            DataAccessCommon.Insert(entity, true,broker);
        //            broker.Commit();
        //        }
        //        catch
        //        {
        //            broker.Rollback();
        //            throw;
        //        }
        //    }
        //}

        public static bool IsAllUserCheck(string strCheckType, string strMainKey)
        {
            return !ClsCommDA.IsAllUserCheck(strCheckType, strMainKey);
        }

        public static void DelCheckRecord(string strSign, string strID)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.DelCheckRecord(strSign, strID, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static void UpdateCheckStatus(string strTblName, string strMainKeyField, string strMainKeyValue)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.UpdateStatus(strTblName, strMainKeyField, strMainKeyValue,"2", broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static bool IsHighVersion(string strBLLFileName, string strMainKey)
        {
            return ClsCommDA.IsHighVersion(strBLLFileName, strMainKey);
        }

        public static bool IsUserHavePower(string strUserID, string strRoleName)
        {
            return ClsCommDA.IsUserHavePower(strUserID, strRoleName);
        }

        public static bool IsCheckBill(string strSign)
        {
            return ClsCommDA.IsCheckBill(strSign);
        }

        public static bool IsViewBill(string strSign, string strUserID)
        {
            return ClsCommDA.IsViewBill(strSign, strUserID);
        }

        public static string SelectFormAuditBillID(string m_strTbl, object objMainKey)
        {
            string strMainKey = CommonMethod.GetString(objMainKey);
            return ClsCommDA.SelectFormAuditBillID(m_strTbl, strMainKey);
        }

        public static string SelectNextCheckUserName(string strFormAuditBillID)
        {
            return ClsCommDA.SelectNextCheckUserName(strFormAuditBillID);
        }

        public static string SelectNextCheckUser(string strFormAuditBillID)
        {
            return ClsCommDA.SelectNextCheckUser(strFormAuditBillID);
        }

        public static bool IsProxy(string strUserID, string strProxyer)
        {
            return ClsCommDA.IsProxy(strUserID, strProxyer);
        }

        public static void UpdateCheckRecord(string strFormAuditBillID, string strIsSuccess, string strFailureSeason, string strCurUserID, string strTbl, string strMainKey, string strMainKeyValue, string strCheckRes)
        {
            ClsCommDA.UpdateCheckRecord(strFormAuditBillID, strIsSuccess, strFailureSeason, strCurUserID, strTbl, strMainKey, strMainKeyValue, strCheckRes);
        }

        public static string GetFailureSeason(string strTbl, string strCode)
        {
            return ClsCommDA.GetFailureSeason(strTbl, strCode);
        }

        public static bool IsFailure(string strTbl, string strCode)
        {
            return ClsCommDA.IsFailure(strTbl, strCode);
        }

        public static string SelectUserByCheckUser(string strCheckUser)
        {
            return ClsCommDA.SelectUserByCheckUser(strCheckUser);
        }

        public static bool IsCheckUser(string m_strTbl, string strStockID, string strFormAuditBillID,string strCurUser)
        {
            return ClsCommDA.IsCheckUser(m_strTbl, strStockID, strFormAuditBillID, strCurUser);
        }

        public static void UpdateProxy(string strCheckUser, string strIsProxy)
        {
            ClsCommDA.UpdateProxy(strCheckUser, strIsProxy);
        }

        public static string GetProxy(string strCheckUser)
        {
            return ClsCommDA.GetProxy(strCheckUser);
        }

        public static void UpdateInCheck(string strCode, bool bReset, string strTbl, string strMainKey, string

strModuleID)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (bReset)
                    {
                        ClsCommDA.DelAuditBill(strTbl, strCode, broker);
                    }
                    ClsCommDA.BillSaveAudit(strTbl, strCode, strModuleID, broker);
                    ClsCommDA.UpdateInCheck(strTbl, strMainKey, strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static bool IsCheck(string strTbl, string strMainKey, string strMainValue)
        {
            return ClsCommDA.IsCheck(strTbl, strMainKey, strMainValue);
        }

        public static DataSet GetMainSet(string strUserID)
        {
            return ClsCommDA.GetMainSet(strUserID);
        }

        public static DataTable GetMainBill(string strBillType)
        {
            return ClsCommDA.GetMainBill(strBillType);
        }

        public static DataTable SelectCheck(string strTbl, string strCode)
        {
            return ClsCommDA.SelectCheck(strTbl, strCode);
        }

        public static void SetDataBaseType(string strDataBaseType)
        {
            DataAccessConfigurationManagement.ConfigType = strDataBaseType;
            DataAccessConfigurationManagement.GetDataAccessConfiguration("");
        }

        public static void ClearDataBase()
        {
            DataAccessConfigurationManagement.ClearCache();
        }

        public static DataTable GetMainWorkExpire(string strUserID)
        {
            return ClsCommDA.GetMainWorkExpire(strUserID);
        }

        public static DataTable SelectPreBig(string strddlBigTypeID,
                                                      int pageIndex,
                                                      int pageSize,
                                                      out int count)
        {
            return ClsCommDA.SelectPreBig(strddlBigTypeID, pageIndex, pageSize, out count);
        }

        public static DataTable SelectPreSmall(string strddlBigTypeID, string strddlSmallTypeID,
                                                      int pageIndex,
                                                      int pageSize,
                                                      out int count)
        {
            return ClsCommDA.SelectPreSmall(strddlBigTypeID, strddlSmallTypeID, pageIndex, pageSize, out count);
        }

        public static DataTable SelStaAluminiumList(string strtxtItemName, string strMatCode, string strMatName)
        {
            return ClsCommDA.SelStaAluminiumList(strtxtItemName, strMatCode, strMatName);
        }

        public static DataTable SelStaGlassList(string strtxtItemName, string strMatCode)
        {
            return ClsCommDA.SelStaGlassList(strtxtItemName, strMatCode);
        }

        public static DataTable SelStaOtherList(string strtxtItemName, string strMatCode, string strMatName)
        {
            return ClsCommDA.SelStaOtherList(strtxtItemName, strMatCode, strMatName);
        }

        public static DataTable SelStaSteelList(string strtxtItemName, string strMatCode, string strMatName)
        {
            return ClsCommDA.SelStaSteelList(strtxtItemName, strMatCode, strMatName);
        }

        public static DataTable SelStaStoneList(string strtxtItemName, string strMatCode)
        {
            return ClsCommDA.SelStaStoneList(strtxtItemName, strMatCode,"");
        }

        public static DataTable SelStaAluSingleList(string strtxtItemName, string strtxtBaseAluSingleID, string strtxtPanelID)
        {
            return ClsCommDA.SelStaAluSingleList(strtxtItemName, strtxtBaseAluSingleID, strtxtPanelID);
        }

        public static DataTable SelStaItemMgrList(string strtxtFeeItemName, string strtxtItemName, string strddllx, string strtxtBillCode)
        {
            return ClsCommDA.SelStaItemMgrList(strtxtFeeItemName, strtxtItemName, strddllx, strtxtBillCode);
        }

        public static DataTable SelProStockList(string strtxtProductName, string strtxtSpecifications)
        {
            return ClsCommDA.SelProStockList(strtxtProductName, strtxtSpecifications);
        }

        public static DataTable SelMatStockList(string strddlBigTypeID, string strddlSmallTypeID, string strddlMatType, string strtxtMatID, string strtxtMatName, string strtxtSpecifications)
        {
            return ClsCommDA.SelMatStockList(strddlBigTypeID, strddlSmallTypeID, strddlMatType, strtxtMatID, strtxtMatName, strtxtSpecifications);
        }

        public static decimal GetStockNum(string strMaterialID)
        {
            return ClsCommDA.GetStockNum(strMaterialID);
        }

        /// <summary>
        /// 取得包装库存量，单位2
        /// </summary>
        /// <param name="strMaterialID"></param>
        /// <returns></returns>
        public static decimal GetStockNum2(string strMaterialID)
        {
            return ClsCommDA.GetStockNum2(strMaterialID);
        }

        public static string GetBillCode(string strTbl, string strMainKey, string strBillPre, int iLen)
        {
            return ClsCommDA.GetBillCode(strTbl, strMainKey, strBillPre, iLen);
        }
        public static string SelectNextCheckUserPro(string strFormAuditBillID, object objCreateUserID, string strModuleID)
        {
            string strCreateUserID = CommonMethod.GetString(objCreateUserID);
            return ClsCommDA.SelectNextCheckUserPro(strFormAuditBillID, strCreateUserID, strModuleID);
        }
        public static bool UpdateInCheck(string strCode, bool bReset, string strTbl, string strMainKey, string strModuleID, string strUserID)
        {
            bool b = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (bReset)
                    {
                        ClsCommDA.DelAuditBill(strTbl, strCode, broker);
                    }
                    b = ClsCommDA.BillSaveAudit(strTbl, strCode, strModuleID, strUserID, broker);

                    //SysFormauditbillEntity entLast = new SysFormauditbillEntity();
                    //entLast.BillType = strTbl;
                    //entLast.BillID = strCode;
                    //entLast.OriCheckUser = "garysun";
                    //entLast.OrderNum = ClsCommDA.GetMaxOrderNum(strTbl, strCode, broker);
                    //DataAccessCommon.Insert(entLast, true, broker);

                    if (b)
                    {
                        ClsCommDA.UpdateInCheck(strTbl, strMainKey, strCode, broker);
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
            return b;
        }

        public static void UpdateIsCheck(string strTbl, string strMainKey, string strMainKeyValue, string IsCheck)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.UpdateIsCheck(strTbl, strMainKey, strMainKeyValue, IsCheck, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }
    }
}
